<?php

class Wangdian extends MY_controller {
	public $left_menu_min;
	function __construct(){
		parent::__construct();
		$this->left_menu_min = array("menu" => "网校设置", "curr" => '/wangdian', "list" => array("学校设置" => "/school", "学校资讯" => "/news", "帮助中心" => "/help", "友情连接" => "/friend_link", "网点管理" => "/wangdian", "问答管理" => "/question_topic", "客服设置" => "/kefu"));
	  	$this->smarty->assign("left_menu_min", $this->left_menu_min);
	  	$this->smarty->assign("menu", 'school');
	}

	function index($name = 0, $limit = 0){
		if($this->input->post()){
			$limit = 0;
			$post_data = $this->input->post();
			$name = $post_data['name'] ? urldecode($post_data['name']) : 0;
		}else{
			$name = urldecode($name);
		}
		$conditions = array('name' => $name,'school_id'=>$this->user_data['school_id']);
		$this->load->model("Wangdians");
		$result = $this->Wangdians->get_wangdian_list($limit, $this->offset, $conditions);
		$this->load->library('pagination');
		$page_config['base_url'] = '/wangdian/index/' . $name;
		$page_config['total_rows'] = $result['count'];
		$page_config['per_page'] = $this->offset;
		$page_config['uri_segment'] = 4;
		$this->pagination->initialize($page_config);
		$pagination = $this->pagination->create_links();
		$this->smarty->assign("action", '网点列表');
		$this->smarty->assign("name", $name);
		$this->smarty->assign("item_list", $result['result']);
		$this->smarty->assign("pagination", $pagination);
		$this->smarty->display("wangdian/list.html");
	}

	function add(){
		$post_data = $this->input->post();
		if($post_data){
			$post_data['add_time'] = time();
			if(empty($post_data['status'])) $post_data['status'] = 0;
			if(!empty($post_data['logo'])){
				$post_data['logo'] = get_qiniu_url($post_data['logo']);
			}
			if (!empty($post_data['url']) && !strstr($post_data['url'], 'http://')){
				$post_data['url'] = 'http://' . $post_data['url'];
			}
			if(isset($post_data['file'])) unset($post_data['file']);
			$post_data['school_id'] = $this->user_data['school_id'];
			$this->db->insert("o2o_school_wangdians", $post_data);
			$insert_id = $this->db->insert_id();
			if($insert_id > 0){
				show_message($this->jump_url);
			}else{
				show_message($this->jump_url, "操作失败");
			}
		}else{
			$this->load->model("Areas");
			$province_list = $this->Areas->get_province_data();
			/*if($school_info['province_code'] > 0){
				$city_list = $this->Areas->get_province_data($school_info['province_code']);
				$this->smarty->assign("city_list", $city_list);
			}

			if($school_info['city_code'] > 0){
				$area_list = $this->Areas->get_province_data($school_info['city_code']);
				$this->smarty->assign("area_list", $area_list);
			}*/
			$this->smarty->assign("province_list", $province_list);
			$this->smarty->assign("action", '添加网点');
			$this->smarty->display("wangdian/save.html");
		}
	}

	function edit($id = 0){
		$post_data = $this->input->post();
		if($post_data){
			if(!empty($post_data['img'])){
				$post_data['img'] = get_qiniu_url($post_data['img']);
			}
			if (!empty($post_data['url']) && !strstr($post_data['url'], 'http://')){
				$post_data['url'] = 'http://' . $post_data['url'];
			}
			if(empty($post_data['status'])) $post_data['status'] = 0;
			if(isset($post_data['file'])) unset($post_data['file']);
			$this->db->where('id', $post_data['id']);
			$this->db->update('o2o_school_wangdians', $post_data);
			if($this->db->affected_rows() > 0){
				show_message($this->jump_url);
			}else{
				show_message($this->jump_url, "操作失败");
			}
		}else{
			if(intval($id) > 0){
				$wangdian_info = get_field_by_where("o2o_school_wangdians", "*", "id='{$id}'");
				if(!$wangdian_info){
					show_message("/help", "数据不存在");exit;
				}
				$this->load->model("Areas");
				$province_list = $this->Areas->get_province_data();
				if($wangdian_info['province_code'] > 0){
					$city_list = $this->Areas->get_province_data($wangdian_info['province_code']);
					$this->smarty->assign("city_list", $city_list);
				}

				if($wangdian_info['city_code'] > 0){
					$area_list = $this->Areas->get_province_data($wangdian_info['city_code']);
					$this->smarty->assign("area_list", $area_list);
				}
				$this->smarty->assign("province_list", $province_list);
				$this->smarty->assign("wangdian_info", $wangdian_info);
				$this->smarty->assign("action", '编辑网点');
				$this->smarty->display("wangdian/save.html");
			}else{
				show_message("/wangdian", "非法操作");
			}
		}
	}

	function delete($ids){
		if($_POST){
			$ids = $this->input->post('ids');
		}else{
			$ids = (array) $ids;
		}
		delete_data($ids, "o2o_school_wangdians");
		if($this->db->affected_rows() > 0){
			show_message($this->jump_url, "删除成功");
		}else{
			show_message($this->jump_url, "删除失败，请重试！");
		}
	}
}